﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using BSCMFF.Core.DataAccessLayer;
using BSCMFF.Core.Exceptions.Database;

namespace BSCMFF.Web.UserControls
{
    /// <summary>
    /// Department selection.
    /// </summary>
    /// <remarks></remarks>
    public partial class SBUSelection : System.Web.UI.UserControl
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Handles the Click event of the Button1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Handles the Click event of the AddButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void AddButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                IRadTreeNodeContainer target = RadTreeView1;
                if (RadTreeView1.SelectedNode != null)
                {
                    RadTreeView1.SelectedNode.Expanded = true;
                    target = RadTreeView1.SelectedNode;
                }
                                
                BSCEntities context = new BSCEntities();
                SBU sbu = new SBU();
                sbu.Name = RadTextBoxNode.Text;
                if (RadTreeView1.SelectedNode != null)
                {
                    sbu.Superior_SBU_FK = Int32.Parse(RadTreeView1.SelectedNode.Value);
                }
                context.AddToSBUs(sbu);

                try
                {
                    context.SaveChanges();

                }
                catch (Exception ex)
                {
                    throw new InconsistentDatabaseException("Couldn't create department: " + ex.Message);
                }

                RadTextBoxNode.Text = "";
                RadTreeNode addedNode = new RadTreeNode(sbu.Name, sbu.SBU_ID.ToString());
                addedNode.Selected = true;
                
                target.Nodes.Add(addedNode);
                addedNode.DataBind();
                RadTreeView1_NodeClick(this, new RadTreeNodeEventArgs(addedNode));
            }
        }

        /// <summary>
        /// Handles the NodeClick event of the RadTreeView1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Telerik.Web.UI.RadTreeNodeEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void RadTreeView1_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
        }

        /// <summary>
        /// Handles the Click event of the RadButtonDeleteNode control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void RadButtonDeleteNode_Click(object sender, EventArgs e)
        {
            RadTreeNode selectedNode = RadTreeView1.SelectedNode;
            
            Page.Validate("DeleteRequiresSelection");
            if (Page.IsValid)
            {
                BSCEntities context = new BSCEntities();
                DeleteDepartment(selectedNode, context);

                try
                {
                    context.SaveChanges();
                }
                catch
                {
                    throw new Exception();
                }

                RadTreeView1.SelectedNode.Remove();
            }            
        }

        /// <summary>
        /// Deletes the department.
        /// </summary>
        /// <param name="selectedNode">The selected node.</param>
        /// <param name="context">The context.</param>
        /// <remarks></remarks>
        private void DeleteDepartment(RadTreeNode selectedNode, BSCEntities context)
        {
            if (selectedNode.Nodes.Count > 0)
            {
                foreach (RadTreeNode n in selectedNode.Nodes)
                {
                    DeleteDepartment(n, context);
                }
            }

            int sbu_id = Int32.Parse(selectedNode.Value);

            SBU sbu = (from s in context.SBUs
                       where s.SBU_ID == sbu_id
                       select s).FirstOrDefault();

            if (sbu != null)
            {
                context.DeleteObject(sbu);  
            }
        }
    }
}